import { createApp } from 'vue'
import App from './App.vue'
import {router} from "./router";
import {store} from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'


const app = createApp(App)

app.use(router)
app.use(store)
app.use(ElementPlus)




import axios from "axios"
axios.defaults.baseURL = 'http://localhost:8443/api'
// 使请求带上凭证信息
axios.defaults.withCredentials = true

app.config.globalProperties.$axios = axios


app.mount('#app')


router.beforeEach((to, from, next) => {
        if (store.state.username && to.path.startsWith('/admin')) {
            initAdminMenu(router, store)
        }
        if (store.state.username && to.path.startsWith('/login')) {
            next({
                name: 'Dashboard'
            })
        }
        // 如果前端没有登录信息则直接拦截，如果有则判断后端是否正常登录（防止构造参数绕过）
        if (to.meta.requireAuth) {
            if (store.state.username) {
                axios.get('/authentication').then(resp => {
                    if (resp) {
                        next()
                    }
                })
            } else {
                next({
                    path: 'login',
                    query: {redirect: to.fullPath}
                })
            }
        } else {
            next()
        }
    }
)

// http response 拦截器
// axios.interceptors.response.use(
//     response => {
//         return response
//     },
//     error => {
//         if (error) {
//             store.commit('logout')
//             router.replace('/login')
//         }
//         // 返回接口返回的错误信息
//         return Promise.reject(error)
//     })

const initAdminMenu = (router, store) => {
    // 防止重复触发加载菜单操作
    if (store.state.adminMenus.length > 0) {
        return
    }
    axios.get('/menu').then(resp => {
        if (resp && resp.status === 200) {
            var fmtRoutes = formatRoutes(resp.data.result)
            console.info(fmtRoutes)
            store.commit('initAdminMenu', fmtRoutes)
            fmtRoutes.forEach(fmtRoute =>{
                router.addRoute(fmtRoute)
            })
            // router.addRoutes(fmtRoutes)
        }
    })
}

const formatRoutes = (routes) => {
    let fmtRoutes = []
    routes.forEach(route => {
        if (route.children) {
            route.children = formatRoutes(route.children)
        }

        let fmtRoute = {
            path: route.path,
            //resolve => {
            //                 require(['./components/admin/' + route.component + '.vue'], resolve)
            //             }
            component: ()=>import('@/components/admin/' +route.component),
            name: route.name,
            nameZh: route.nameZh,
            iconCls: route.iconCls,
            meta: {
                requireAuth: true
            },
            children: route.children
        }
        fmtRoutes.push(fmtRoute)
    })
    return fmtRoutes
}
